package com.loong.musicdemo.ui.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Album;
import com.loong.musicdemo.ui.find.activity.ShowAlbumPlayListActivity;

import java.util.ArrayList;
import java.util.List;

public class MineAlbumAdapter extends RecyclerView.Adapter<MineAlbumAdapter.ViewHolder> {
    private List<Album> albumList;
    private Context context;

    public MineAlbumAdapter(List<Album> albumList, Context context) {
        this.albumList = albumList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.newest_album_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Album album = albumList.get(position);
        Glide.with(context).load(album.getAlbumPicUrl()).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(holder.album_img);
        holder.album_name.setText(album.getAlbumName());
        holder.album_artist.setText(album.getAlbumArtistName());
        holder.albumView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Album a = albumList.get(holder.getAdapterPosition());
                Intent intent = new Intent(context, ShowAlbumPlayListActivity.class);
                intent.putExtra("albumName", a.getAlbumName());
                intent.putExtra("albumImg", a.getAlbumPicUrl());
                intent.putExtra("albumIDs", (ArrayList)a.getSongIdsInAlbum());
                intent.putExtra("albumCreatorName", a.getAlbumArtistName());
                intent.putExtra("albumCreatorPic", a.getAlbumArtistPicUrl());
                intent.putExtra("albumCommentCount", a.getAlbumCommentCount());
                intent.putExtra("albumPlayCount", "0");
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return albumList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView album_img;
        TextView album_name,album_artist;
        View albumView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            albumView = itemView;
            album_img = itemView.findViewById(R.id.album_img);
            album_name = itemView.findViewById(R.id.album_name);
            album_artist = itemView.findViewById(R.id.album_artist);
        }
    }
}
